package cn.iocoder.yudao.module.vote.controller.admin.fielddynamicsconfig;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.fielddynamicsconfig.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.fielddynamicsconfig.FieldDynamicsConfigDO;
import cn.iocoder.yudao.module.vote.convert.fielddynamicsconfig.FieldDynamicsConfigConvert;
import cn.iocoder.yudao.module.vote.service.fielddynamicsconfig.FieldDynamicsConfigService;

@Api(tags = "管理后台 - 表动态配置字段展示")
@RestController
@RequestMapping("/vote/field-dynamics-config")
@Validated
public class FieldDynamicsConfigController {

    @Resource
    private FieldDynamicsConfigService fieldDynamicsConfigService;

    @PostMapping("/create")
    @ApiOperation("创建表动态配置字段展示")
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:create')")
    public CommonResult<Long> createFieldDynamicsConfig(@Valid @RequestBody FieldDynamicsConfigCreateReqVO createReqVO) {
        return success(fieldDynamicsConfigService.createFieldDynamicsConfig(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新表动态配置字段展示")
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:update')")
    public CommonResult<Boolean> updateFieldDynamicsConfig(@Valid @RequestBody FieldDynamicsConfigUpdateReqVO updateReqVO) {
        fieldDynamicsConfigService.updateFieldDynamicsConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除表动态配置字段展示")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:delete')")
    public CommonResult<Boolean> deleteFieldDynamicsConfig(@RequestParam("id") Long id) {
        fieldDynamicsConfigService.deleteFieldDynamicsConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得表动态配置字段展示")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:query')")
    public CommonResult<FieldDynamicsConfigRespVO> getFieldDynamicsConfig(@RequestParam("id") Long id) {
        FieldDynamicsConfigDO fieldDynamicsConfig = fieldDynamicsConfigService.getFieldDynamicsConfig(id);
        FieldDynamicsConfigRespVO respVO = FieldDynamicsConfigConvert.INSTANCE.convert(fieldDynamicsConfig);
        respVO.setFieldInfoList(JSONObject.parseArray(respVO.getFieldList(),FieldInfoVO.class));
        respVO.setFieldList(null);
        respVO.setOrderFrontList(JSONObject.parseArray(fieldDynamicsConfig.getOrderList(),FieldInfoVO.class));
        return success(respVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得表动态配置字段展示列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:query')")
    public CommonResult<List<FieldDynamicsConfigRespVO>> getFieldDynamicsConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<FieldDynamicsConfigDO> list = fieldDynamicsConfigService.getFieldDynamicsConfigList(ids);
        return success(FieldDynamicsConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得表动态配置字段展示分页")
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:query')")
    public CommonResult<PageResult<FieldDynamicsConfigRespVO>> getFieldDynamicsConfigPage(@Valid FieldDynamicsConfigPageReqVO pageVO) {
        PageResult<FieldDynamicsConfigDO> pageResult = fieldDynamicsConfigService.getFieldDynamicsConfigPage(pageVO);

        PageResult<FieldDynamicsConfigRespVO> result = FieldDynamicsConfigConvert.INSTANCE.convertPage(pageResult);

        for(FieldDynamicsConfigRespVO respVO:result.getList()){
            respVO.setFieldInfoList(JSONObject.parseArray(respVO.getFieldList(),FieldInfoVO.class));
            respVO.setFieldList(null);
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出表动态配置字段展示 Excel")
    @PreAuthorize("@ss.hasPermission('vote:field-dynamics-config:export')")
    @OperateLog(type = EXPORT)
    public void exportFieldDynamicsConfigExcel(@Valid FieldDynamicsConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FieldDynamicsConfigDO> list = fieldDynamicsConfigService.getFieldDynamicsConfigList(exportReqVO);
        // 导出 Excel
        List<FieldDynamicsConfigExcelVO> datas = FieldDynamicsConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "表动态配置字段展示.xls", "数据", FieldDynamicsConfigExcelVO.class, datas);
    }

}
